package com.bestcem.xm.ticket.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.deliver.grpc.v1.services.*;
import com.bestcem.xm.ticket.grpc.client.convert.TicketBaseDeliverConvert;
import com.bestcem.xm.ticket.grpc.client.dto.deliver.SMSBalanceDTO;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;
import zhongyan.xm.base.v1.Base;

import javax.annotation.Nullable;
import java.util.Objects;

/**
 * 工单调用 deliver 模块相关rpc接口客户端
 *
 * @author ympan <yuming.pan@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/31 11:15
 */
@Slf4j
@Service
public class TicketDeliverGrpcService {

    @GrpcClient(GrpcClientNames.DS_DELIVER)
    private BpDeliverTicketServiceGrpc.BpDeliverTicketServiceBlockingStub bpDeliverTicketServiceBlockingStub;

    /**
     * 工单创建二次投放
     *
     * @param againDeliverTaskId 二次投放任务ID
     * @param deliverId          投放ID
     * @param ticketId           工单ID
     * @param seq                答卷编号
     * @param memberId           联系人ID
     * @param orgId              组织ID
     * @param surveyId           问卷ID
     * @return 二次投放 ID
     * @author <yuming.pan@idiaoyan.com>
     * @date 11:41 2021/8/31
     */
    @Nullable
    public String createTicketAgainDeliver(String againDeliverTaskId,
                                           String deliverId,
                                           String ticketId,
                                           Long seq,
                                           String memberId,
                                           String orgId,
                                           String surveyId) {
        CreateTicketAgainDeliverRequest.Builder builder = CreateTicketAgainDeliverRequest.newBuilder();
        builder.setAgainDeliverTaskId(againDeliverTaskId);
        builder.setDeliverId(deliverId);
        builder.setTicketId(ticketId);
        builder.setSeq(seq);
        builder.setMemberId(memberId);
        builder.setOrgId(orgId);
        builder.setSurveyId(surveyId);
        try {
            CreateTicketAgainDeliverResponse response =
                    bpDeliverTicketServiceBlockingStub.createTicketAgainDeliver(builder.build());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] DELIVER createTicketAgainDeliver {},{},{},{},{},{},{} Error; ErrorMsg: {}",
                        againDeliverTaskId, deliverId, ticketId, seq, memberId, orgId, surveyId, response);
                return null;
            }
            return response.getId();
        } catch (Exception e) {
            log.error("[Ticket] DELIVER createTicketAgainDeliver {},{},{},{},{},{},{} Error; ErrorMsg: {}",
                    againDeliverTaskId, deliverId, ticketId, seq, memberId, orgId, surveyId, e.getMessage(), e);
            return null;
        }
    }

    /**
     * 根据 org_id 获取 签名
     *
     * @param orgId 公司id
     * @return 签名
     * @author <yuming.pan@idiaoyan.com>
     * @date 13:37 2021/8/31
     */
    @Nullable
    public String getSignByOrgId(String orgId) {
        GetSignByOrgIdRequest.Builder builder = GetSignByOrgIdRequest.newBuilder();
        builder.setOrgId(orgId);
        try {
            GetSignByOrgIdResponse response =
                    bpDeliverTicketServiceBlockingStub.getSignByOrgId(builder.build());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] DELIVER getSignByOrgId {} Error; ErrorMsg: {}", orgId, response);
                return null;
            }
            return response.getSign();
        } catch (Exception e) {
            log.error("[Ticket] DELIVER getSignByOrgId {} Error; ErrorMsg: {}", orgId, e.getMessage(), e);
            return null;
        }
    }

    /**
     * 根据org_id 获取 短信余额
     *
     * @param orgId 组织id
     * @return 短信余额
     * @author <yuming.pan@idiaoyan.com>
     * @date 13:58 2021/8/31
     */
    @Nullable
    public SMSBalanceDTO getSmsBalanceByOrgId(String orgId) {
        GetSmsBalanceByOrgIdRequest.Builder builder = GetSmsBalanceByOrgIdRequest.newBuilder();
        builder.setOrgId(orgId);
        try {
            GetSmsBalanceByOrgIdResponse response =
                    bpDeliverTicketServiceBlockingStub.getSmsBalanceByOrgId(builder.build());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] DELIVER getSmsBalanceByOrgId {} Error; ErrorMsg: {}", orgId, response);
                return null;
            }
            return TicketBaseDeliverConvert.infoToSMSBalanceDTO(response.getBalance());
        } catch (Exception e) {
            log.error("[Ticket] DELIVER getSmsBalanceByOrgId {} Error; ErrorMsg: {}", orgId, e.getMessage(), e);
            return null;
        }
    }
}
    